import plugin from '../../lib/plugins/plugin.js'
import common from "../../lib/common/common.js";
import YAML from "yaml"
import fs from "fs"

let file = `config/config/group.yaml`
export class jinyong extends plugin {
    constructor() {
        super({
            name: "禁用功能",
            dsc: "控制机器人开关",
            event: "message",
            priority: 1000, //指令优先级
            rule: [
			{
                reg: '^#?(群)?(取消)?禁用功能(.*)$', 
                fnc: "offapp",
            },
			{
                reg: '^#?bot(取消)?禁用功能(.*)$', 
                fnc: "botoffapp",
            },
			{
                reg: '^#?查看(群|bot)?禁用功能(.*)$', 
                fnc: "chapp",
            },
			{
                reg: '^#?(取消)?启用功能(.*)$', 
                fnc: "onlyoffapp",
            },
			{
                reg: '^#?启用全部功能$', 
                fnc: "onlyapps",
            },
			{
                reg: '^#?查看启用功能(.*)$', 
                fnc: "chaonlypp",
            },
			{
                reg: '^#?全局(取消)?禁用功能(.*)$', 
                fnc: "offappall",
            },
			{
                reg: '^#?查看全局禁用功能(.*)$', 
                fnc: "chappall",
            },
           ]
        });
    }
 
  async offapp(e) {
	if (!e.isGroup || !e.isMaster) return
	let groupId = e.group_id; // 获取群聊号
	let ids = `${this.e.self_id}:${groupId}`
    
        let key = e.msg.replace(/#|(群)?(取消)?禁用功能/g, '').trim()
		let data = YAML.parse(fs.readFileSync(file, 'utf8'));
		if (!data[ids]) {
		    data[ids] = {}
	    }
		if (!data[ids].disable) {
		    data[ids].disable = []
		}
		if (e.msg.includes(`取消`)) {
			if (!data[ids].disable.includes(key)) {
				e.reply(`此群没有禁用功能 ${key}`,false,{ recallMsg: 10 })
				return
			}
		    let index = data[ids].disable.indexOf(key);
            if (index > -1) {
                data[ids].disable.splice(index, 1);
                await fs.writeFileSync(file, YAML.stringify(data));
                e.reply(`已在此群取消禁用功能 ${key}`, false, { recallMsg: 10 });
            }
		}
		else {
			if (data[ids].disable.includes(key)) {
				e.reply(`此群已禁用功能 ${key}，无需重复操作`,false,{ recallMsg: 10 })
				return
			}
			if (data[`default`].disable.length > 0) {
				for (let dd of data[`default`].disable) {
					if (!data[ids].disable.includes(dd) && dd != key) {
				        data[ids].disable.push(dd)
					}
				}
			}
			    data[ids].disable.push(key)
			await fs.writeFileSync(file, YAML.stringify(data));
			
			e.reply(`已在此群禁用功能 ${key}`,false,{ recallMsg: 10 })
		}
		return true
    }
	
  async botoffapp(e) {
	if (!e.isGroup || !e.isMaster) return
	let groupId = e.group_id; // 获取群聊号
	let ids = `${this.e.self_id}:default`
	
        let key = e.msg.replace(/#|bot(取消)?禁用功能/g, '').trim()
		let data = YAML.parse(fs.readFileSync(file, 'utf8'));
		if (!data[ids]) {
		    data[ids] = {}
	    }
		if (!data[ids].disable) {
			data[ids].disable = []
		}
		if (e.msg.includes(`取消`)) {
			if (!data[ids].disable.includes(key)) {
				e.reply(`此bot没有禁用功能 ${key}`,false,{ recallMsg: 10 })
				return
			}
		let index = data[ids].disable.indexOf(key);
            if (index > -1) {
            data[ids].disable.splice(index, 1);
            await fs.writeFileSync(file, YAML.stringify(data));
            e.reply(`此bot已取消禁用功能 ${key}`, false, { recallMsg: 10 });
           }
		}
		else {
			if (data[ids].disable.includes(key)) {
				e.reply(`此bot已禁用功能 ${key}，无需重复操作`,false,{ recallMsg: 10 })
				return
			}
			if (data[`default`].disable.length > 0) {
				for (let dd of data[`default`].disable) {
					if (!data[ids].disable.includes(dd) && dd != key) {
				        data[ids].disable.push(dd)
					}
				}
			}
			    data[ids].disable.push(key)
			await fs.writeFileSync(file, YAML.stringify(data));
			
			e.reply(`此bot已禁用功能 ${key}`,false,{ recallMsg: 10 })
		}
		return true
    }	

  async chapp(e) {
	if (!e.isGroup || !e.isMaster) return
	let groupId = e.group_id; // 获取群聊号
	let ids
	if (e.msg.includes(`bot`)) {
		ids = `${this.e.self_id}:default`
	} else {
		ids = `${this.e.self_id}:${groupId}`
	}
	
		let data = YAML.parse(fs.readFileSync(file, 'utf8'));
			if (!data[ids]) {
				e.reply(`暂未禁用任何功能`,false,{ recallMsg: 10 })
				return
			} 
			if (!data[ids].disable) {
				e.reply(`暂未禁用任何功能`,false,{ recallMsg: 10 })
				return
			}
			if (data[ids].disable.length == 0) {
				e.reply(`暂未禁用任何功能`,false,{ recallMsg: 10 })
				return
			}
		let app = []
		for (let i = 0; i < data[ids].disable.length; i++) {
			app.push(`${i+1}. ${data[ids].disable[i]}\n`)
		}
		await e.reply(await common.makeForwardMsg(e,[app]));
		 
		return true
    }

   async onlyoffapp(e) {
	if (!e.isMaster) return
	let ids = `${this.e.self_id}:default`
	
        let key = e.msg.replace(/#|(取消)?启用功能/g, '').trim()
		let data = YAML.parse(fs.readFileSync(file, 'utf8'));
		if (!data[ids]) {
		    data[ids] = {}
	    }
		if (!data[ids].enable) {
			data[ids].enable = []
		}
		if (e.msg.includes(`取消`)) {
			if (!data[ids].enable.includes(key)) {
				e.reply(`当前bot没有启用功能 ${key}`,false,{ recallMsg: 10 })
				return
			}
		let index = data[ids].enable.indexOf(key);
            if (index > -1) {
            data[ids].enable.splice(index, 1);
            await fs.writeFileSync(file, YAML.stringify(data));
            e.reply(`当前bot已取消启用功能 ${key}`, false, { recallMsg: 10 });
           }
		}
		else {
			if (data[ids].enable.includes(key)) {
				e.reply(`当前bot已启用功能 ${key}，无需重复操作`,false,{ recallMsg: 10 })
				return
			}
		    if (!data[ids].enable.includes('禁用功能')) {
			    data[ids].enable.push('禁用功能')
			}
			    data[ids].enable.push(key)
			await fs.writeFileSync(file, YAML.stringify(data));
			
			e.reply(`当前bot已启用功能 ${key}`,false,{ recallMsg: 10 })
		}
			return true
    }
	
  async onlyapps(e) {	
    if (!e.isMaster) return
    let ids = `${this.e.self_id}:default`
	
		let data = YAML.parse(fs.readFileSync(file, 'utf8'));
		    if (!data[ids]) {
				e.reply(`当前bot已启用全部功能`,false,{ recallMsg: 10 })
				return
			} 
			if (!data[ids].enable) {
				e.reply(`当前bot已启用全部功能`,false,{ recallMsg: 10 })
				return
			}
			if (data[ids].enable.length == 0) {
				e.reply(`当前bot已启用全部功能`,false,{ recallMsg: 10 })
				return
			}
			data[ids].enable = []
			await fs.writeFileSync(file, YAML.stringify(data));
			
			e.reply(`当前bot已启用全部功能`,false,{ recallMsg: 10 })
			
		return true	
  }

  async chaonlypp(e) {
	if (!e.isMaster) return
    let ids = `${this.e.self_id}:default`
	
		let data = YAML.parse(fs.readFileSync(file, 'utf8'))
			if (!data[ids]) {
				e.reply(`当前bot已启用全部功能`,false,{ recallMsg: 10 })
				return
			} 
			if (!data[ids].enable) {
				e.reply(`当前bot已启用全部功能`,false,{ recallMsg: 10 })
				return
			}
			if (data[ids].enable.length == 0) {
				e.reply(`当前bot已启用全部功能`,false,{ recallMsg: 10 })
				return
			}
		let app = []
		for (let i = 0; i < data[ids].enable.length; i++) {
			app.push(`${i+1}. ${data[ids].enable[i]}\n`)
		}
		await e.reply(await common.makeForwardMsg(e,[app]));
		 
		return true
    }	

  async offappall(e) {
	if (!e.isMaster) return
	
        let key = e.msg.replace(/#|全局(取消)?禁用功能/g, '').trim()
		let data = YAML.parse(fs.readFileSync(file, 'utf8'))
		if (e.msg.includes(`取消`)) {
			if (!data[`default`].disable.includes(key)) {
				e.reply(`全局没有禁用功能 ${key}`,false,{ recallMsg: 10 })
				return
			}
		let index = data[`default`].disable.indexOf(key);
            if (index > -1) {
            data[`default`].disable.splice(index, 1);
            await fs.writeFileSync(file, YAML.stringify(data));
            e.reply(`已全局取消禁用功能 ${key}`, false, { recallMsg: 10 });
           }
		}
		else {
			if (data[`default`].disable.includes(key)) {
				e.reply(`已全局禁用功能 ${key}，无需重复操作`,false,{ recallMsg: 10 })
				return
			}
			    data[`default`].disable.push(key)
			await fs.writeFileSync(file, YAML.stringify(data));
			
			e.reply(`已全局禁用功能 ${key}`,false,{ recallMsg: 10 })
		}
			return true
    }	

  async chappall(e) {
	if (!e.isMaster) return
	
		let data = YAML.parse(fs.readFileSync(file, 'utf8'))
			if (data[`default`].disable.length == 0 || data[`default`].disable.length == null) {
				e.reply(`全局没有禁用任何功能`,false,{ recallMsg: 10 })
				return
			}
		let app = []
		for (let i = 0; i < data[`default`].disable.length; i++) {
			app.push(`${i+1}. ${data['default'].disable[i]}\n`)
		}
		await e.reply(await common.makeForwardMsg(e,[app]));
		 
		return true
    }	

  }